//
//  RechargeController.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/6.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "RechargeController.h"
#import "RechargeCell.h"
#import "FinishRechargeController.h"

@interface RechargeController ()<UITableViewDelegate,UITableViewDataSource,RechargeDelegete>

@property (nonatomic , strong) UITableView * tableview;
@property (nonatomic , strong) UITextField * tf;
@property (nonatomic , strong) UILabel * label;

@property (nonatomic , strong) NSMutableArray * array;
@property (nonatomic , strong) UIButton * payForBtn;

@end

@implementation RechargeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setNavi
{
    self.title = @"钱包充值";
}

-(void)setUI
{
    self.tableview = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStyleGrouped)];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    [self.tableview registerNib:[UINib nibWithNibName:@"RechargeCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell0"];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    view.backgroundColor = [UIColor clearColor];
    self.payForBtn = [[UIButton alloc]init];
    [view addSubview:self.payForBtn];
    [self.payForBtn setTitle:@"立即支付" forState:(UIControlStateNormal)];
    [self.payForBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [self.payForBtn setBackgroundColor:MainBtnColor];
    self.payForBtn.userInteractionEnabled = NO;
    [self.payForBtn addTarget:self action:@selector(payFor) forControlEvents:(UIControlEventTouchUpInside)];
    self.payForBtn.frame = CGRectMake(15, 0, kScreenWidth - 30, 40);
    self.payForBtn.layer.masksToBounds = YES;
    self.payForBtn.layer.cornerRadius = 20;
    
    self.tableview.tableFooterView = view;
    
    self.array = [NSMutableArray array];
}

#pragma mark 立即支付
- (void)payFor
{
    if (self.tf.text.length >= 1) {
        FinishRechargeController * vc = [[FinishRechargeController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return 80;
        
    }else
    {
        return 65;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell0" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.contentView addSubview:self.tf];
        [cell addSubview:self.label];
        
        return cell;
        
    }else
    {
        RechargeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell bindDataWithIndex:indexPath.row];
        cell.delegate = self;
        [self.array addObject:cell.selectedBtn];
        
        return cell;
    }
    return nil;
}

- (void)btnClickWithTag:(NSInteger)tag
{
    [self.view endEditing:YES];
    for (int i = 0; i < self.array.count; i ++) {
        UIButton * btn = self.array[i];
        if (btn.tag != tag) {
            btn.selected = NO;
        }else
        {
            btn.selected = YES;
            NSLog(@"%ld",(long)btn.tag);
        }
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return @"支付方式";
    }else
    {
        return @"充值金额";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else {
        return 3;
    }
}

-(UILabel *)label
{
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(15, 40 - 13, 20, 26)];
        _label.text = @"¥";
        _label.font = FontSize(37);
    }
    return _label;
}

-(UITextField *)tf
{
    if (!_tf) {
        _tf = [[UITextField alloc]initWithFrame:CGRectMake(60, 0, kScreenWidth, 80)];
        _tf.borderStyle = UITextBorderStyleNone;
        _tf.keyboardType = UIKeyboardTypeNumberPad;
        _tf.font = FontSize(20);
        [_tf addTarget:self action:@selector(textChanged) forControlEvents:(UIControlEventEditingChanged)];
    }
    return _tf;
}

- (void)textChanged
{
    if (self.tf.text.length >= 1) {
        self.payForBtn.userInteractionEnabled = YES;
        [self.payForBtn setBackgroundColor:MainColor];
    }else
    {
        [self.payForBtn setBackgroundColor:MainBtnColor];
        self.payForBtn.userInteractionEnabled = NO;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
